package com.github.minglie.mingapiutils.utils;

import java.util.HashMap;
import java.util.Map;

public class ApiUtils {

    public static String  runApi(String apiName,Map map){
        String code=DbUtils.getByApiName(apiName);
        String[] split = code.split("Request request = new Request.Builder");
        code="Request request = new Request.Builder"+split[1];
        String result = CustomStringJavaCompiler.runJava(ApiUtilsConstants.BEAFORE_OK_HTTP + code + ApiUtilsConstants.END_OK_HTTP,map);
        return result;
    }
    public static String  runApi(String apiName){
        String result = runApi(apiName, null);
        return result;
    }

    public static void main(String[] args) {
        String code="OkHttpClient client = new OkHttpClient().newBuilder()\n" +
                "  .build();\n" +
                "Request request = new Request.Builder()\n" +
                "  .url(\"http://localhost:8888/miApiListByPage?startPage=1&limit=1\")\n" +
                "  .method(\"GET\", null)\n" +
                "  .addHeader(\"authorization\", \"abcdefg\")\n" +
                "  .addHeader(\"userId\", \"1607616657000\")\n" +
                "  .build();\n" +
                "Response response = client.newCall(request).execute();";

        String[] split = code.split("Request request = new Request.Builder");
        System.out.println(split);
        code="Request request = new Request.Builder"+split[1];
        System.out.println(code);
        Map map=new HashMap();
        map.put("limit", "2");
        String s = CustomStringJavaCompiler.runJava(ApiUtilsConstants.BEAFORE_OK_HTTP + code + ApiUtilsConstants.END_OK_HTTP,map);
        System.out.println(s);
    }



}
